package net.tv90.bfmccore.code;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.File;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

public class BattlepassMenuGenerator {


    public static void main(String[] args) {
        generateMenu();
    }

    public static void writeToFile(String yamlStr) {
        try {
            File parent = new File(new File("./plugins/BFMCCore"), "/menu");
            if (!parent.exists()) {
                parent.mkdirs();
            }
            File file = new File(parent, "test.yml");
            try (Writer writer = new OutputStreamWriter(Files.newOutputStream(file.toPath()), StandardCharsets.UTF_8)) {
                writer.write(yamlStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Data
    @AllArgsConstructor
    static class Item {
        String itemName;
        String itemId;
        String itemShortName;
    }

    public static Item[] getItems() {
        return new Item[]{
                new Item("93R手枪", "flansmod_93r", "93r"),
                new Item("LATV4 轻型侦察车[T0]", "mcheli_latv4_762", "latv4_t0"),
                new Item("G3A3突击步枪", "flansmod_g3a3", "g3a3"),
                new Item("神威无敌大将军火炮", "mcheli_oldcannon", "oldcannon"),
                new Item("单兵联合主动防御设备", "flansmod_portableads", "portableads"),
                new Item("MIM-23 霍克 中程导弹防御系统", "mcheli_mim23", "mcheli_mim23"),
                new Item("SR-2冲锋枪", "flansmod_sr2", "sr2"),
                new Item("军用掩体", "flansmod_blockblindage", "blindage"),
                new Item("LATV4 轻型侦察车[T1]", "mcheli_latv4_127", "latv4_t1"),
                new Item("AMB-17卡宾步枪", "flansmod_amb17", "amb17"),
                new Item("Schipunov 42 金属风暴发射器", "mcheli_metalstorm", "metalstorm"),
                new Item("MK3A1霰弹枪", "flansmod_mk3a1", "mk3a1"),
                new Item("QN202单兵导弹发射器[AP]", "flansmod_qn202", "qn202ap"),
                new Item("柳叶刀巡飞弹", "mcheli_lancet", "lancet"),
                new Item("LATV4 轻型侦察车[T2]", "mcheli_latv4_40", "latv4_t2"),
                new Item("MDR卡宾步枪", "flansmod_mdr", "mdr"),
                new Item("MT-12SM 前线火炮", "mcheli_MT-12SM", "mt12"),
                new Item("防爆军用掩体", "flansmod_blockadvancedblindage", "advancedblindage"),
                new Item("XFAD-4 德鲁格 隐身无人轰炸机", "mcheli_S70", "xfad4"),
                new Item("LATV4 轻型侦察车[T3]", "mcheli_latv4_50", "latv4_t3"),
                new Item("HT-95 列夫科夫", "mcheli_HT-95", "ht95"),
                new Item("黄金战斗包", "", ""),
                new Item("黄金战斗包", "", ""),
                new Item("黄金战斗包", "", ""),
                new Item("黄金战斗包", "", ""),
        };
    }

    private static void generateMenu() {
        final String yamlStr =
                "X{coordx}Y{coordy}:\n" +
                "  NAME: \"领取通行证{level}级奖励\"\n" +
                "  LORE:\n" +
                "    - '{itemName}'\n" +
                "    - '你的通行证等级为%battlepass_level%，总点数为%battlepass_point%，还需%battlepass_upgrade%点数升至下一级'\n" +
                "    - '%battlepass_query_{level}%'\n" +
                "    - '%battlepass_reward_{level}%'\n" +
                "  ID: {itemId}\n" +
                "  PLACEHOLDERAPI: true\n" +
                "  DATA-VALUE: 0\n" +
                "  KEEP-OPEN: true\n" +
                "  PRICE: 0\n" +
                "  AUTO-REFRESH: 1\n" +
                "  COMMAND: \"console:bps giveitem {player} {level} unlock.{itemShortName}\"\n" +
                "  POSITION-X: {coordx}\n" +
                "  POSITION-Y: {coordy}";
        final String yamlStr2 =
                "X{coordx}Y{coordy}:\n" +
                "  NAME: \"领取通行证{level}级奖励\"\n" +
                "  LORE:\n" +
                "    - '1000游戏币'\n" +
                "    - '你的通行证等级为%battlepass_level%，总点数为%battlepass_point%，还需%battlepass_upgrade%点数升至下一级'\n" +
                "    - '%battlepass_query_{level}%'\n" +
                "    - '%battlepass_reward_{level}%'\n" +
                "  ID: flansmod_money\n" +
                "  PLACEHOLDERAPI: true\n" +
                "  DATA-VALUE: 0\n" +
                "  KEEP-OPEN: true\n" +
                "  PRICE: 0\n" +
                "  AUTO-REFRESH: 1\n" +
                "  COMMAND: \"console:bps givemoney {player} {level} 1000\"\n" +
                "  POSITION-X: {coordx}\n" +
                "  POSITION-Y: {coordy}";
        final String yamlStr3 =
                "X{coordx}Y{coordy}:\n" +
                "  NAME: \"领取通行证{level}级奖励\"\n" +
                "  LORE:\n" +
                "    - '500军衔'\n" +
                "    - '你的通行证等级为%battlepass_level%，总点数为%battlepass_point%，还需%battlepass_upgrade%点数升至下一级'\n" +
                "    - '%battlepass_query_{level}%'\n" +
                "    - '%battlepass_reward_{level}%'\n" +
                "  ID: flansmod_rank\n" +
                "  PLACEHOLDERAPI: true\n" +
                "  DATA-VALUE: 0\n" +
                "  KEEP-OPEN: true\n" +
                "  PRICE: 0\n" +
                "  AUTO-REFRESH: 1\n" +
                "  COMMAND: \"console:bps giverank {player} {level} 500\"\n" +
                "  POSITION-X: {coordx}\n" +
                "  POSITION-Y: {coordy}";
        final String yamlStr4 =
                "X{coordx}Y{coordy}:\n" +
                "  NAME: \"领取通行证{level}级奖励\"\n" +
                "  LORE:\n" +
                "    - '白银战斗包'\n" +
                "    - '你的通行证等级为%battlepass_level%，总点数为%battlepass_point%，还需%battlepass_upgrade%点数升至下一级'\n" +
                "    - '%battlepass_query_{level}%'\n" +
                "    - '%battlepass_reward_{level}%'\n" +
                "  ID: flansmod_bp_silver\n" +
                "  PLACEHOLDERAPI: true\n" +
                "  DATA-VALUE: 0\n" +
                "  KEEP-OPEN: true\n" +
                "  PRICE: 0\n" +
                "  AUTO-REFRESH: 1\n" +
                "  COMMAND: \"console:bps givebattlepack {player} {level} silver\"\n" +
                "  POSITION-X: {coordx}\n" +
                "  POSITION-Y: {coordy}";
        String exactYamlStr = yamlStr;
        StringBuilder result = new StringBuilder();
        //int level = 1;
        //int index = -1;
        int level = 54;
        int index = 13;
        Item[] items = getItems();
        for (int j = 1; j <= 6; j++) {
            for (int i = 1; i <= 9; i++) {

                if ((level - 1) % 4 == 0) {
                    exactYamlStr = yamlStr;
                    index++;
                } else if ((level - 2) % 4 == 0) {
                    exactYamlStr = yamlStr2;
                } else if ((level - 3) % 4 == 0) {
                    exactYamlStr = yamlStr3;
                } else if (level % 4 == 0) {
                    exactYamlStr = yamlStr4;
                }

                result.append(exactYamlStr
                        .replace("{coordx}", String.valueOf(i))
                        .replace("{coordy}", String.valueOf(j))
                        .replace("{level}", String.valueOf(level))
                        .replace("{itemName}", items[index].getItemName())
                        .replace("{itemId}", items[index].getItemId())
                        .replace("{itemShortName}", items[index].getItemShortName()));
                result.append('\n');

                level++;

                if(level > 100) {
                    break;
                }
            }
        }
        writeToFile(result.toString());
    }
}
